﻿using System.Linq;
using System.Web.Security;
using BluePortal.QuikStart.LiveID;

namespace BluePortal.LiveID
{
    public class BluePortalMemberShipProvider : SqlMembershipProvider
    {
        public override bool DeleteUser(string username, bool deleteAllRelatedData)
        {
            LiveIdService lidService = new LiveIdService();
            lidService.DeleteLoginName(username);
            return base.DeleteUser(username, deleteAllRelatedData);
        }

        public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
        {
            return base.CreateUser(username, password, email, passwordQuestion, passwordAnswer, isApproved, providerUserKey, out status);
        }

        public void ChangeAvatar(string username, byte[] image)
        {
            BOWebDataContext context = new BOWebDataContext();
            Avatar avatar = context.Avatars.SingleOrDefault(u => u.loginName == username);
            if (avatar == null)
            {
                avatar = new Avatar() { loginName = username };
                context.Avatars.InsertOnSubmit(avatar);
            }
            avatar.Image = image;
            context.SubmitChanges();
        }

        public byte[] GetAvatar(string username)
        {
            BOWebDataContext context = new BOWebDataContext();
            Avatar avatar = context.Avatars.Single(u => u.loginName == username);

            return avatar.Image;
        }
    }
}
